<?php
namespace app\store\model\store;

use app\common\model\BaseModel;
use app\common\model\User as UserModel;

class Perform extends BaseModel{
    // 定义表名
    protected $name = 'perform';

    // 定义主键
    protected $pk = 'id';

    //百分比数值加上百分号
    public function getAwardAttr($value){
        return $value.'%';
    }

    public function getAward2Attr($value){
        return $value.'%';
    }

    public function add(array $data){
        return $this->save([
            'store_id' => self::$storeId,
            'gradeid' => $data['gradeid'],
            'award' => $data['award'],
            'gradeid2' => $data['gradeid2'],
            'award2' => $data['award2'],
            'performance' => $data['performance'],
            'des' => $data['des']
        ]);
    }

    //默认数据
    public function enumData($userGradeModel){
        $selectInfo = $userGradeModel->where(['status'=>1,'is_delete'=>0])->field('grade_id,name')->select()->toArray();
        array_push($selectInfo,[
            'grade_id' => 0,
            'name' => '请选择会员级别'
        ]);
        return [
            'store_id' => self::$storeId,
            'gradeid' => 0,
            'award' => 15,
            'gradeid2' => 0,
            'award2' => 17,
            'selectInfo' => $selectInfo,
            'performance' => 1000000,
            'des' => '酒类业绩呈现'
        ];
    }

    //获取第一条信息
    public function detail($data){
        $id = $data['id'] ?? 0;
        $r = (new Perform)->where(['store_id'=>self::$storeId,'id'=>$id])->findOrEmpty();
        $userGradeModel = new UserGrade();
        if($r->isEmpty()){
            return $this->enumData($userGradeModel);
        }else{
            $arr = $r->getData();
            $grade_id = $arr['gradeid'];
            $name = $userGradeModel->where(['grade_id'=>$grade_id,'status'=>1,'is_delete'=>0])->value('name');
            $arr['select'] = [
                'grade_id'=>$grade_id,
                'name' => $name
            ];
            $grade_id2 = $arr['gradeid2'];
            $name2 = $userGradeModel->where(['grade_id'=>$grade_id2,'status'=>1,'is_delete'=>0])->value('name');
            $arr['select2'] = [
                'grade_id'=>$grade_id2,
                'name' => $name2
            ];
            $selectInfo = $userGradeModel->where(['status'=>1,'is_delete'=>0])->field('grade_id,name')->select()->toArray();
            array_push($selectInfo,[
                'grade_id' => 0,
                'name' => '请选择会员级别'
            ]);
            $arr['selectInfo'] = $selectInfo;
            return $arr;
        }
    }

    //更新第一条信息
    public function edit($data){
        $r = self::where(['id' => $data['id']])->find();
        $award = str_replace('%','',$data['award']);
        $award2 = str_replace('%','',$data['award2']);
        return $r->save([
            'store_id' => self::$storeId,
            'gradeid'=>$data['gradeid'],
            'award' => $award,
            'gradeid2'=>$data['gradeid2'],
            'award2' => $award2,
            'performance' => $data['performance'],
            'des' => $data['des']
        ]);
    }

    public function getGradeidAttr($value){
        $grade_id = $value;
        if($grade_id == 0) {
            return '-';
        }
        $userGradeModel = new UserGrade();
        return $userGradeModel->where(['grade_id'=>$grade_id])->value('name');
    }

    public function getGradeid2Attr($value){
        $grade_id = $value;
        if($grade_id == 0) {
            return '-';
        }
        $userGradeModel = new UserGrade();
        return $userGradeModel->where(['grade_id'=>$grade_id])->value('name');
    }

    //获取数据列
    public function lists($where){
        $limit = $where['limit'];
        $query = self::where(['status'=>$where['status']]);
        if(isset($where['des'])){
            return $query->whereLike('des',"%{$where['des']}%")
                ->paginate($limit);
        }
        return $query->paginate($limit);
    }
    //删除数据
    public function del($data)
    {
        $id = $data['id'];
        return $this->where(['id'=>$id])->delete();
    }
    //设置商品状态
    public function setStatus($param){
        $status = $param['status'];
        $status = $status==1?0:1;
        return $this->where(['id'=>$param['id']])->save(['status'=>$status]);
    }
}
